package ellements.drawable;

import matrix.world.volleyball.MySurfaceView;
import matrix.world.volleyball.R;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Align;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import ellements.Ball;

public class Desk {
	private Paint pa = new Paint();
	
	private Bitmap mBitmap,mBitmapG;
	private Bitmap bmVolleyBallNetFront;
	private Bitmap bmSmallBall;
	
	public boolean needdraw = true;
	
	public final static float dTop = 52*MySurfaceView.rate;
	public final static float dLeft = 16*MySurfaceView.rate;
    public final static float dRight = 24*MySurfaceView.rate;
    public final static float NetLeft = MySurfaceView.Width*0.5f-dLeft;
    public final static float NetRight= MySurfaceView.Width*0.5f+dRight;
    public final static float NetTop  = 203*MySurfaceView.rate;
	
    private final Rect clear = new Rect(
    		(int)(MySurfaceView.Width/2 - 105*MySurfaceView.rate) ,
    		0,
    		(int)(MySurfaceView.Width/2 + 105*MySurfaceView.rate),
    		(int)(70*MySurfaceView.rate));
    
	public Desk(Resources res){
		pa.setColor(Color.YELLOW);
		pa.setTextAlign(Align.CENTER);
		Typeface tf = Typeface.createFromAsset(res.getAssets(),"fonts/OfficalTyrant17 Bold.TTF");
		pa.setTypeface(tf);
		pa.setTextSize(28*MySurfaceView.rate);
		
		Matrix matrix = new Matrix();
		matrix.postScale(MySurfaceView.rate, MySurfaceView.rate);
		
		mBitmapG = BitmapFactory.decodeResource(res, R.drawable.desk);
		mBitmapG = Bitmap.createBitmap(mBitmapG, 0, 0, mBitmapG.getWidth(), mBitmapG.getHeight(), matrix, true);
		
		Matrix matrixSmall = new Matrix();
		matrixSmall.postScale(0.5f, 0.5f);
		bmSmallBall = BitmapFactory.decodeResource(res, R.drawable.ball);
		bmSmallBall = Bitmap.createBitmap(bmSmallBall, 0, 0, bmSmallBall.getWidth(), bmSmallBall.getHeight(), matrixSmall, true);
		bmSmallBall = Bitmap.createBitmap(bmSmallBall, 0, 0, bmSmallBall.getWidth(), bmSmallBall.getHeight(), matrix, true);

		mBitmap = Bitmap.createBitmap(MySurfaceView.Width, MySurfaceView.Height, Config.RGB_565);
		Canvas c = new Canvas(mBitmap);
		
		Rect src = new Rect(0, 0, 1,mBitmapG.getHeight());
		Rect dst = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
		c.drawBitmap(mBitmapG, src, dst,null);
		c.drawBitmap(mBitmapG, (MySurfaceView.Width-mBitmapG.getWidth())/2f, (MySurfaceView.Height-mBitmapG.getHeight())/2f, null);
		mBitmapG = mBitmap;
		
		bmVolleyBallNetFront = BitmapFactory.decodeResource(res, R.drawable.volleyballnet_front);
		bmVolleyBallNetFront = Bitmap.createBitmap(bmVolleyBallNetFront, 0, 0, bmVolleyBallNetFront.getWidth(), bmVolleyBallNetFront.getHeight(), matrix, true);
		
		updateSceneClear();
		updateScene();
	}

    

	public void draw(Canvas c){
		c.drawBitmap(mBitmap, 0, 0, null);
	}

	public void drawFront(Canvas c){
		c.drawBitmap(bmVolleyBallNetFront, MySurfaceView.Width/2 - 16*MySurfaceView.rate,202*MySurfaceView.rate, null);
	}
	
	
	public void updateSceneGameOver(){
		Thread thread = new Thread(new Runnable() {
			public void run() {
				Canvas c = new Canvas(mBitmap);
				c.drawBitmap(mBitmapG, clear,clear, null);
				c.drawText("GAM OVER", MySurfaceView.Width*0.5f, MySurfaceView.Height*0.1f,pa);
				c.drawText("FINAL SCORE", MySurfaceView.Width*0.5f, MySurfaceView.Height*0.2f,pa);
				c.drawText(String.valueOf(MySurfaceView.SCORE), MySurfaceView.Width*0.5f, MySurfaceView.Height*0.3f,pa);
				c.drawText("PLAY AGAIN",MySurfaceView.Width*0.5f, MySurfaceView.Height*0.4f,pa);
			}
		});
		thread.start();
	}

	public void updateScene() {
		Thread thread = new Thread(new Runnable() {
			public void run() {
				Canvas c = new Canvas(mBitmap);
				c.drawBitmap(mBitmapG, clear,clear, null);
				float smallX=MySurfaceView.Width/2-90*MySurfaceView.rate;
				
				float smallY=12*MySurfaceView.rate;
				for(int i=0;i<Ball.ballLiveNumber;i++){
					c.drawBitmap(bmSmallBall, smallX, smallY,null);
//					c.drawCircle(smallX, smallY, bmSmallBall.getWidth()/2, pa);
					smallX+=20*MySurfaceView.rate;
				}
				c.drawText(MySurfaceView.SCORE+"", MySurfaceView.Width/2, 65*MySurfaceView.rate, pa);
			}
		});
		thread.start();
	}

	public void updateSceneClear() {
		mBitmap = mBitmapG.copy(Config.RGB_565, true);
	}
}
